<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>写评论</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        table, tr, th, td {
            border: 1px solid #5fbdcb;
        }

        table {
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div>
    请输入评论内容:<label for="content"></label>
    <input type="text" name="content" id="content" placeholder="请输入评论内容"><br>
    <input type="button" id="button" value="提交">
    <a id="a">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    function getParam(url, name) {
        try {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = url.split('?')[1].match(reg);
            if (r != null) {
                return r[2];
            }
            return "";
        } catch (e) {
            return "";
        }
    }

    let url = window.location.href;
    let storeId = getParam(url, 'storeId');
    let commentId = getParam(url, 'commentId');
    $(function () {
        $.ajax({
            url: "http://localhost:8080/ban.do",
            type: "post",
            data: {
                method: 'judgeBan'
            },
            dataType: "text",
            success: function (data) {
                if (data === "error") {
                    alert("您被封禁，不能评论");
                    window.location.href = '/User/UserCommentPage.jsp?storeId=' + storeId;
                }
                if (data === "login") {
                    alert("请先登录！")
                    window.location.href = 'CommentPage.jsp?storeId=' + storeId;
                }
            }
        })
        let a = document.querySelector("#a");
        a.href = '/User/UserCommentPage.jsp?storeId=' + storeId;
        $(':button').on('click', function () {
            $.ajax({
                url: "http://localhost:8080/comment.do",
                type: "post",
                data: {
                    storeId: storeId,
                    content: $('#content').val(),
                    commentId: commentId,
                    method: 'doComment'
                },
                dataType: "text",
                success: function (data) {
                    if (data === 'ok') {
                        alert("评论成功");
                        window.location.href = '/User/UserCommentPage.jsp?storeId=' + storeId;
                    } else {
                        alert("评论失败");
                    }
                }
            })
        })
    })
</script>
</body>
</html>
